package com.atguigu.cloud.controller;

import cn.hutool.core.date.DateUtil;
import com.atguigu.cloud.apis.PayFeignApi;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.resp.ReturnCodeEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author bairimengchang
 * @version 1.0
 * @project cloud2024
 * @description 核心控制器
 * @date 2024/9/12
 */
@RestController
@Slf4j
public class OrderController {

    @Resource
    private PayFeignApi payFeignApi;

    // 新增订单
    // 这里原本是GetMapping
    @PostMapping("/consumer/pay/add")
    public ResultData<String> addOrder(PayDTO payDTO){
        // return restTemplate.postForObject(PaymentSrv_URL + "/pay/add", payDTO, ResultData.class);
        log.info("这里进行了一些列新增订单数据的操作，此时需要新增一条流水信息的操作");
        return payFeignApi.addPay(payDTO);
    }

    // 按照流水ID查订单流水
    @GetMapping("/consumer/pay/get/{id}")
    public ResultData<PayDTO> getPayInfo(@PathVariable("id") Integer id){
        // return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/"+id, ResultData.class, id);
        log.info("这里进行了一系列查询订单数据的操作，此时需要查询一条流水信息的操作");
        ResultData<PayDTO> resultData = null;
        try
        {
            System.out.println("调用开始-----:"+DateUtil.now());
            resultData = payFeignApi.getById(id);
            return resultData;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("调用结束-----:"+ DateUtil.now());
            ResultData.fail(ReturnCodeEnum.RC500.getCode(),e.getMessage());
        }
        return null;
    }

    // 查询所有订单流水
    @GetMapping("/consumer/pay/getAll")
    public ResultData<List<PayDTO>> getAll(){
        // return restTemplate.getForObject(PaymentSrv_URL + "/pay/getAll", ResultData.class);
        log.info("这里进行了一系列查询订单数据的操作，此时需要查询全部流水信息的操作");
        return payFeignApi.getAll();
    }

    // 按照ID删除流水
    @DeleteMapping("/consumer/pay/del/{id}")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id){
        /*
        return restTemplate.exchange(
                PaymentSrv_URL + "/pay/del/"+id,
                HttpMethod.DELETE,
                null,
                ResultData.class
        ).getBody();
        */
        log.info("这里进行了一系列删除订单数据的操作，此时需要删除一条流水信息的操作");
        return payFeignApi.deletePay(id);
    }

    // 修改支付流水
    @PutMapping("/consumer/pay/update")
    public ResultData<String> updatePay(PayDTO payDTO){
        /*
        HttpEntity<PayDTO> payDTOHttpEntity = new HttpEntity<>(payDTO);
        return restTemplate.exchange(
                PaymentSrv_URL + "/pay/update",
                HttpMethod.PUT,
                payDTOHttpEntity,
                ResultData.class
        ).getBody();
        */
        log.info("这里进行了一系列修改订单数据的操作，此时需要修改一条流水信息的操作");
        return payFeignApi.updatePay(payDTO);
    }

    @Value("${server.port}")
    private String port;

    @GetMapping(value = "/pay/get/info")
    private String getInfoByConsul(@Value("${cleft.info}") String cleftInfo) {
        return "cleftInfo: "+cleftInfo+"\t"+"port: "+port;
    }

    @GetMapping(value = "/consumer/pay/get/info")
    private String getInfoByConsul() {
        // return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/info", String.class);
        return payFeignApi.getInfoByConsul();
    }


}